from django.db import models

# e your models here.
# 会员信息表
class Users(models.Model):
    # 必选信息，放在主表
    username = models.CharField(max_length = 50)
    password = models.CharField(max_length = 80)
    phone   =  models.CharField(max_length = 11)

    # 可选信息，放在副表
    email   =  models.CharField(max_length = 80,null = True)
    age     =  models.IntegerField(null=True)
    sex     =  models.CharField(max_length = 1,null = True)
    pic     =  models.CharField(max_length = 100,null = True)
    # 状态，0-允许使用 1-禁用
    isDelete  =  models.BooleanField(default=False)
    addtime =  models.DateTimeField(auto_now_add = True)
    # 检查更方便
    def __str__(self):
        return self.name

    # 自定义权限
    class Meta:
        permissions = (
            ("show_users", "查看会员权限"),
            ("insert_users", "添加会员权限"),
            ("edit_users", "编辑会员权限"),
            ("del_users", "删除会员权限"),
        )

# 商品分类模型
class Types(models.Model):
    name = models.CharField(max_length = 20)
    pid = models.IntegerField()
    # 路径记录上级、上上级、。。的pid，否则就要用到递归
    path = models.CharField(max_length = 20)
    
    def __str__(self):
        return self.name
     # 自定义权限
    class Meta:
        permissions = (
            ("show_types", "查看商品分类权限"),
            ("insert_types", "添加商品分类权限"),
            ("edit_types", "编辑商品分类权限"),
            ("del_types", "删除商品分类权限"),
        )
    

# 商品模型
class Goods(models.Model):
    tid = models.ForeignKey(to="Types", to_field="id")

    title = models.CharField(max_length = 100)

    price = models.DecimalField(max_digits = 10, decimal_places=2)
    store = models.IntegerField()

    info = models.TextField()

    pic = models.CharField(max_length = 100)

    clicknum = models.IntegerField(default =0)
    ordernum = models.IntegerField(default = 0)
    # 1 新发布  2 热卖  0缺货 3 卖断
    status = models.IntegerField(default = 1)

    isDelete = models.BooleanField(default = False)

    addtime = models.DateTimeField(auto_now_add = True)

    def __str__(self):
        return self.name

    # 自定义权限
    class Meta:
        permissions = (
            ("show_goods", "查看商品权限"),
            ("insert_goods", "添加商品权限"),
            ("edit_goods", "编辑商品权限"),
            ("del_goods", "删除商品权限"),
        )


# 购物车模型
class Cart(models.Model):
    uid = models.ForeignKey(to='Users',to_field='id')
    gid = models.ForeignKey(to='Goods',to_field='id')
    num = models.IntegerField()
    addtime = models.DateTimeField(auto_now_add=True)



# 收货地址模型
class Address(models.Model):
    uid = models.ForeignKey(to='Users',to_field='id')
    name = models.CharField(max_length=10)
    phone = models.CharField(max_length=11)
    address = models.CharField(max_length=50)
    isSelect =  models.BooleanField(default=False)


# 订单模型
class Order(models.Model):
    uid = models.ForeignKey(to='Users',to_field='id')
    addressid = models.ForeignKey(to='Address',to_field='id')
    totalprice = models.FloatField()
    # 0已取消 1 未支付 2 已支付,待发货  3 已发货,待收货  4 以收货 待评价
    status = models.IntegerField()
    addtime = models.DateTimeField(auto_now_add=True)

    class Meta:
        permissions = (
            ("show_order", "查看订单权限"),
        )


# 订单详情
class OrderInfo(models.Model):
    orderid = models.ForeignKey(to='Order',to_field='id')
    goodsid = models.ForeignKey(to='Goods',to_field='id')
    num = models.IntegerField()
    price = models.FloatField()


